/**
 * 针对uniapp数据缓存优化,可控制数据缓存时间
 * 
 *
 */
const postfix = '_simpleCache'; // 缓存前缀

export default {
	put(key, data, time) {
		uni.setStorageSync(key, data)
		var seconds = parseInt(time);
		if (seconds > 0) {
			var timestamp = Date.parse(new Date());
			timestamp = timestamp / 1000 + seconds;
			uni.setStorageSync(key + postfix, timestamp + "")
		} else {
			uni.removeStorageSync(key + postfix)
		}
	},
	get(key, def) {
		var deadtime = parseInt(uni.getStorageSync(key + postfix))
		if (deadtime) {
			if (parseInt(deadtime) < Date.parse(new Date()) / 1000) {
				if (def) {
					return def;
				} else {
					uni.removeStorageSync(key + postfix)
					uni.removeStorageSync(key)
					return false;
				}
			}
		}
		var res = uni.getStorageSync(key);
		if (res) {
			return res;
		} else {
			if (def == undefined || def == "") {
				def = false;
				uni.removeStorageSync(key)
			}
			return def;
		}
	},
	remove(key) {
		uni.removeStorageSync(key);
		uni.removeStorageSync(key + postfix);
	},
	check(key, remove = false) {
		let res = this.get(key);
		if (remove) this.remove(key);
		return res;
	},
	clear() {
		uni.clearStorageSync();
	}
}

